package com.zhuangfei.hpuhelper.view;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.lzf.serviceplatform.R;

import com.hpu.bean.Course;
import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.service.ShareUploadService;
import com.hpu.tools.ShareTools;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zhuangfei.hpuhelper.bean.ShareDataBean;
import com.zhuangfei.hpuhelper.tools.ActivityTools;
import com.zhuangfei.hpuhelper.tools.CourseTools;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.hpuhelper.tools.FileTools;
import com.zhuangfei.hpuhelper.tools.ViewTools;
import com.zhuangfei.smartalert.core.ImageAlert;
import com.zhuangfei.smartalert.core.MessageAlert;
import com.zhuangfei.smartalert.listener.OnImageAlertListener;
import com.zhuangfei.smartalert.listener.OnMessageAlertAdapter;
import com.zhuangfei.smartalert.listener.OnMessageAlertListener;
import com.zhuangfei.toolkit.tools.ToastTools;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CourseManagerActivity extends Activity implements OnClickListener {

	private LinearLayout courseContainerLayout;
	private LinearLayout backLayout;
	private LinearLayout addLayout;

	MessageAlert messageAlert;

	private LinearLayout allCheckLayout;
	private LinearLayout deleteLayout;
	private LinearLayout shareLayout;
	private CheckBox allCheckBox;

	List<CheckBox> boxs = new ArrayList<>();
	List<Course> courses = new ArrayList<>();
	List<Course> courseList = new ArrayList<>();
	List<View> views = new ArrayList<>();

	private String term;
	private ImageAlert imageAlert;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_course_manager);
		ActivityTools.setTransparent(this);
		initView();
		initEvent();
		addCourseItemView();
		ShareTools.put(this, "course_update", 1);
	}

	private void initEvent() {
		backLayout.setOnClickListener(this);
		addLayout.setOnClickListener(this);
		allCheckLayout.setOnClickListener(this);
		deleteLayout.setOnClickListener(this);
		shareLayout.setOnClickListener(this);
	}

	private void initView() {
		courseContainerLayout = (LinearLayout) findViewById(R.id.id_course_manager_container);
		backLayout = (LinearLayout) findViewById(R.id.id_back);
		addLayout = (LinearLayout) findViewById(R.id.id_add);
		allCheckLayout = (LinearLayout) findViewById(R.id.id_manager_allcheck);
		deleteLayout = (LinearLayout) findViewById(R.id.id_manager_alldelete);
		shareLayout = (LinearLayout) findViewById(R.id.id_manager_share);
		allCheckBox = (CheckBox) findViewById(R.id.id_box_allcheck);
		messageAlert = new MessageAlert(this).create();
		imageAlert=new ImageAlert(this).create();
		
	}

	private void addCourseItemView() {
		courseContainerLayout.removeAllViews();
		term = (String) ShareTools.get(this, ShareConstants.STORE_TERM, "当前学期");
		courseList = CourseTools.getCourseList(this, term);
		LinearLayout containerLayout;
		TextView nameTextView, roomTextView, weeksTextView, dayTextView, teacherTextView;

		for (int i = 0; i < courseList.size(); i++) {
			final Course course = courseList.get(i);
			final View view = LayoutInflater.from(this).inflate(R.layout.item_coursemanager_layout, null);
			containerLayout = (LinearLayout) view.findViewById(R.id.id_container);
			nameTextView = (TextView) view.findViewById(R.id.id_manager_name);
			roomTextView = (TextView) view.findViewById(R.id.id_manager_room);
			weeksTextView = (TextView) view.findViewById(R.id.id_manager_weeks);
			dayTextView = (TextView) view.findViewById(R.id.id_manager_day);
			teacherTextView = (TextView) view.findViewById(R.id.id_manager_teacher);
			final CheckBox checkBox = (CheckBox) view.findViewById(R.id.id_manager_check);

			checkBox.setTag(view);
			boxs.add(checkBox);

			
			containerLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (checkBox.isChecked()) {
						checkBox.setChecked(false);
						courses.remove(course);
					} else {
						checkBox.setChecked(true);
						courses.add(course);
					}
					
					if(courseList.size()!=0&&courseList.size()==courses.size()){
						allCheckBox.setChecked(true);
					}else{
						allCheckBox.setChecked(false);
					}
				}
			});
			// deleteTextView.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View arg0) {
			//
			// }
			// });

			nameTextView.setText(course.getName());
			roomTextView.setText("教室\t" + course.getRoom());
			String s = "";
			List<Integer> weekList = course.getWeekList();
			for (int j = 0; j < weekList.size(); j++) {
				s += weekList.get(j);
				if(j!=(weekList.size()-1)){
					s+=",";
				}
			}

			weeksTextView.setText("周次\t" + s);
			dayTextView.setText("节次\t周" + getDay(course.getDay()) + ",\t" + course.getStart() + "-"
					+ (course.getStart() + course.getStep() - 1) + "节");
			teacherTextView.setText("教师\t" + course.getTeacher());

			courseContainerLayout.addView(view);
		}
	}

	public String getDay(int day) {
		String str = "一二三四五六七";
		return str.charAt(day - 1) + "";
	}

	@Override
	public void onBackPressed() {
		ActivityTools.toBackActivityAnim(this, MenuActivity.class);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.id_back:
			ActivityTools.toBackActivityAnim(CourseManagerActivity.this, MenuActivity.class);
			break;

		case R.id.id_add:
			ActivityTools.toActivity(CourseManagerActivity.this, AddCourseActivity.class);
			finish();
			break;
		case R.id.id_manager_allcheck:
			allCheck();
			break;
		case R.id.id_manager_alldelete:
			if(courses.size()==0) Toast.makeText(this, "没有选中的课程",Toast.LENGTH_SHORT).show();
			else{
				messageAlert.setCancelEnable(true).setTitle("删除课程").setContent("你是否确定要删除选中的课程?")
					.setOnMessageAlertListener(new OnMessageAlertAdapter() {
						@Override
						public void onConfirm(MessageAlert messageAlert) {
							allDelete();
							messageAlert.hide();
						}
					}).show();
			}
			break;
		case R.id.id_manager_share:
			if(courses.size()==0) Toast.makeText(this, "没有选中的课程",Toast.LENGTH_SHORT).show();
			else{
				String json=CourseTools.getCourseJsonString(courses);
				if(TextUtils.isEmpty(json)){
					ToastTools.show(this,"待分享课程为空");
				}else{
					ActivityTools.toActivityAnimByExtras(this, ShareDetailActivity.class,"json", json);
					finish();
				}
				//				shareUploadService.doUpload(json);
			}
			break;
		default:
			break;
		}
	}
	
	private void allDelete() {
		List<Course> cList=new ArrayList<>();
		for (int i = 0; i < courses.size(); i++) {
			Course c = courses.get(i);
			cList.add(c);
		}
		courses.clear();
		courseList.removeAll(cList);
		CourseTools.saveCourse(this, courseList, term);
		allCheckBox.setChecked(false);
		Toast.makeText(this, "删除成功!",Toast.LENGTH_SHORT).show();
		addCourseItemView();
	}

	private void allCheck() {
		if (allCheckBox.isChecked()) {
			for (int i = 0; i < boxs.size(); i++) {
				CheckBox checkBox = boxs.get(i);
				if (checkBox.isChecked()) {
					checkBox.setChecked(false);
					courses.remove(courseList.get(i));
				}
			}
			allCheckBox.setChecked(false);
		} else {
			for (int i = 0; i < boxs.size(); i++) {
				CheckBox checkBox = boxs.get(i);
				if (!checkBox.isChecked()) {
					checkBox.setChecked(true);
					courses.add(courseList.get(i));
				}
			}
			allCheckBox.setChecked(true);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(messageAlert!=null){
			messageAlert.dimiss();
		}
	}
	
	
}
